//会员管理控制。
Ext.define("ES.controller.MembershipController",{
    extend:"Ext.app.Controller",
    models:["MembershipModel"],
    stores:["MembershipStore","MembershipCatalogTreeStore"],
    views:["membership.MembershipList","membership.MembershipEdit"],
    requires:["ES.common"],
    init:function(){
        this.control({
            "membershipTabList":{
                afterrender:function(cmp){
                     var btn = cmp.down("toolbar button[action=btnQuery]");
                     this.onSearch(btn);
                },
                itemdblclick:this.onEdit
            },
            "membershipTabList button[action=btnQuery]":{
                click:this.onSearch
            },
            "membershipTabList button[action=btnAdd]":{
                click:this.onAdd
            },
            "membershipTabList button[action=btnDelete]":{
                click:this.onDelete
            },
            "membershipEdit button[action=btnSave]":{
                click:this.onSave
            },
            "membershipEdit button[action=btnCancel]":{
                click:function(btn){
                     var win = btn.up("membershipEdit");
                     if(win){
                         win.close();
                     }
                }
            }
        })
    },
    onSearch:function(btn){
        var toolbar =  btn.ownerCt;
        var grid = toolbar.ownerCt;       
        var st = grid.getStore();
        st.loadPage(1,{
            params:{
                memberName:toolbar.getComponent(1).getValue()
            }
        });
    },
    onAdd:function(){
         var win = Ext.widget("membershipEdit");
         if(win){
            var tv = win.down("treepanel");
            if(tv){
                tv.getStore().load({
                     params:{memberID:""}
                });
            }
            win.show();
         }
    },
    onEdit:function(view, record, item,index,e,opt){
         var win = Ext.widget("membershipEdit");
         if(win){
                var frm = win.down("form");
                if(frm){
                    var tv = win.down("treepanel");
                    if(tv){
                        tv.getStore().load({
                            params:{memberID:record.data.memberID}
                        });
                    }
                    frm.loadRecord(record);
                }
                win.show();
         }
     },
     onSave:function(btn){
        var win = btn.up("membershipEdit");
        if(win){
            var form = win.down("form").getForm();
            if(form && form.isValid()){
                var mcs = [];
                var tv = win.down("treepanel");
                if(tv){
                    var data = tv.getSelectionModel().getSelection();
                    if(data != null && data.length > 0){
                        Ext.Array.each(data, function(val){
                            mcs.push(val.data.id+","+ Ext.Date.format(val.data.createTime,"Y-m-d")+","+val.data.cycle);
                        });
                    }
                }
                var store = this.getMembershipStoreStore();
                var record = form.getRecord();
                var values = form.getValues();
                if(values){
                    Ext.Object.merge(values,{memberCatalogs:mcs.join(";")});
                }
                if(record){
                    record.set(values);
                }else{
                    record = this.getMembershipModelModel().create();
                    record.set(values);
                    store.insert(0,record);
                }
               store.sync();
            }
            win.close();
        }
     },
     onDelete:function(btn){
         Ext.MessageBox.confirm("删除提示","确认要删除选中的数据吗？", function(cfm){
            if(cfm == "yes"){
                var grid = btn.ownerCt.ownerCt;
                var data = grid.getSelectionModel().getSelection();
                if(data.length == 0){
                        Ext.Msg.alert("提示","您最少要选择一条数据!");
                }else{
                        var st = grid.getStore();
                        Ext.Array.each(data, function(record){
                            st.remove(record);
                        })
                        st.sync();
                }
            }
        });
     }
})